﻿using NB.MGJ.Common;
using NB.MGJ.Common.Helper;
using NB.MGJ.Common.MVC;
using NB.MGJ.Common.OSS;
using NB.MGJ.Model;
using NB.MGJ.Model.CodeManage;
using NB.MGJ.Service;
using NB.MGJ.Service.CodeManage;
using NB.MGJ.Service.Product;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;


namespace NB.MGJ.Web.Controllers
{
    /// <summary>
    /// 生码管理
    /// 昝小念
    /// 2017-9-21
    /// </summary>
    public partial class CodeManageController : BaseController
    {
        public DBHService _dbhService = new DBHService();
        public LeadCodeService _Lecodeservice = new LeadCodeService();
        public GenCodeBatchService _codebatchservice = new GenCodeBatchService();
        private CodeManageService _CodeManageService = new CodeManageService();
        private CardService cardService = new CardService();
        private JsonHelper jsonHelper = new JsonHelper();
        private SearchCodeService scodeService = new SearchCodeService();
        private ProductService _productService = new ProductService();
        private ProductBatchService _ProductBatchService = new ProductBatchService();
        private CodeRelateService _CodeRelateService = new CodeRelateService();
        private Tts_Service tts = new Tts_Service();
        private ApplyCodeService acservive = new ApplyCodeService();
        private ManagerService _ManagerService=new ManagerService();
        private CodeCommonService _CodeCommonService=new CodeCommonService();

        #region 生码管理
        
        /// <summary>
        ///详细数据
        ///修改 2017年12月13日10:20:55
        ///Pass 1
        /// </summary>
        public ActionResult RawShowData(int id)
        {
            ViewBag.model = _CodeManageService.GetCodeModelId(id);
            //ViewBag.model = model;
            return View();
        }

        public ActionResult RawCode()
        {
            return View();
        }

        /// <summary>
        /// 新增弹出窗
        /// </summary>
        public ActionResult BornCode()
        {
            return View();
        }

        public ActionResult downloadCode()
        {
            return View();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public ActionResult EditCode(int id)
        {
            return View(_CodeManageService.GetCodeModelId(id));
        }

        /// <summary>
        /// 根据机构其他下拉框数据
        ///Pass 1
        /// </summary>
        public JsonResult Getproductlist(int Eid)
        {
            return Json(_CodeManageService.GetproductlistService(Eid), JsonRequestBehavior.AllowGet);
        }

        //Pass 1
        public JsonResult GetTypehlist(int Pid)
        {
            return Json(_CodeManageService.GetTypehlistService(Pid), JsonRequestBehavior.AllowGet);
        }

        //Pass 1
        public JsonResult GetBatchlist(int Pid)
        {
            return Json(_CodeManageService.GetBatchlistService(Pid), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增
        /// 真实环境下，语音生成处
        /// tts.Tts(filename, model.Query_Tel_Content, model.Voice);
        /// 需接收返回数据，及其它处理
        /// Pass 1
        /// 2017年12月13日11:49:57
        /// </summary>
        public int AddRowCode(CodeManage_GenCode model, string batch, int allotid)
        {
            if (batch.Length == 2)
            {
                batch = "00" + batch;
            }
            else if (batch.Length == 3)
            {
                batch = "0" + batch;
            }


            model.Batch = model.Batch.PadLeft(4, '0');
            CodeManage_GenCodeBatch Batch = new CodeManage_GenCodeBatch();
            Batch.EnterpriseId = model.EnterpriseId;
            Batch.ProductId = Convert.ToInt32(model.ProductId);
            Batch.Batch = model.Batch;
            Batch.ViewFlag = 1;
            Batch.Sequence = 9999;
            Batch.ProductBatchId = model.ProductBatchId;
            Batch.ChangeLoginID = LogOnUser.UserID;
            Batch.ChangeLoginName = LogOnUser.LoginName;
            Batch.ChangeDate = DateTime.Now;
            Batch.TypeID = model.TypeID;
            int result = _CodeManageService.AddGenCodebatch(Batch);
            string Ecode = _CodeManageService.Ecode(model.EnterpriseId);
            string filename = Ecode + Batch.Batch + ".voc";

            CodeManage_Card Card = new CodeManage_Card();
            model.Batch = null;

            Card.EnterpriseId = model.EnterpriseId;
            Card.BatchID = result;
            Card.ChangeDate = DateTime.Now;
            Card.ChangeLoginName = LogOnUser.LoginName;
            Card.ChangeLoginID = LogOnUser.UserID;
            cardService.Add(Card);
            //创建生码任务表信息
            model.PatchId = result;
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.Status = 0;//待生码状态
            model.ViewDel = 0;
            model.Voicefile = filename;
            if (model.ExpiryDate == "0")//无限期
            {
                model.DisableTime = DateTime.MaxValue;
            }
            else
            {
                model.DisableTime = DateTime.Now.AddYears(int.Parse(model.ExpiryDate));
            }
            tts.Tts(filename, model.Query_Tel_Content, model.Voice);
            if (allotid != 0)
            {
                var re = _CodeManageService.GetAllotCount(model, batch, allotid);
            }
            result = _CodeManageService.AddGenCode(model);
            return result;
        }

        // Pass 1
        public string GetCodBatch(int EnterpriseId)
        {
            return _CodeManageService.GetCbatchservice(EnterpriseId);
        }

        /// <summary>
        /// 编辑      
        /// 数据体积问题
        /// 2017年12月13日13:24:33
        /// </summary>
        public int EditRowCode(CodeManage_GenCode model)
        {
            
            CodeManage_GenCodeBatch Batch = new CodeManage_GenCodeBatch();
            Batch.ProductId = Convert.ToInt32(model.ProductId);
            Batch.ProductBatchId = model.ProductBatchId;
            Batch.Batch = model.Batch;
            Batch.TypeID = model.TypeID;
            Batch.Sequence = 0;
            Batch.ViewFlag = 1;
            Batch.ChangeLoginID = LogOnUser.UserID;
            Batch.Id = model.PatchId;
            Batch.ChangeLoginName = LogOnUser.EnterpriseName;
            Batch.EnterpriseId = model.EnterpriseId;
           int rulsets = _CodeManageService.Updproduct(Batch);
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            int rulset = _CodeManageService.UpdCode(model);
            return rulset;
        }
        /// <summary>
        /// 下拉框绑定	
        /// </summary>
        /// <param name="psearch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public JsonResult SelEnterpriseBox()
        {
            ManagerService _manager = new ManagerService();
            List<Organization_Enterprise> result = _manager.GetSonEnterprise(LogOnUser.EnterpriseId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SelnameList(int Eid)
        {
            List<Product_Product> result = _CodeManageService.GetSelnameListboxService(Eid);
            return Json(result);
        }
        public JsonResult SeltypeList(int Pid)
        {
            List<Product_Product> result = _CodeManageService.GetSeltypeListboxService(Pid);
            return Json(result);
        }
        public JsonResult SelBatchList(int Pid)
        {
            List<Product_Batch> result = _CodeManageService.GetSelBatchListboxService(Pid);
            return Json(result);
        }
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="rsearch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public JsonResult GetRowCode(DetailSearch Smodel, int pageIndex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            //查询语句判断
            if (LogOnUser.Distributor != 0)
            {
                pm.StrCondition = "  w.ChangeLoginID=" + LogOnUser.UserID;
            }
            else
            {
                if (LogOnUser.EnterpriseId != 1)
                {
                    if (string.IsNullOrEmpty(LogOnUser.JuniorEnterprise))
                        pm.StrCondition = "  w.EnterpriseId  in (0)";
                    else
                        pm.StrCondition = "  w.EnterpriseId in (" + LogOnUser.JuniorEnterprise + ")";
                }
                else pm.StrCondition = "  1=1 ";

            }


            var result = _CodeManageService.GetRowCode(ref pm, Smodel);
            return Json(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int DelRowCode(int id)
        {
            int result = _CodeManageService.DelRowCodeService(id);
            return result;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        public ActionResult Execls(string key)
        {
            var model = _CodeManageService.GetAllGenCode(key);
            var _byte_batch = Excel.Exprot<CodeManage_GenCode>(model.ToList().Where(p => p.ViewDel == 0).ToList(), new string[] { "公司名称", "机构代码", "批次", "生码数量", "网站查询", "电话查询", "短信查询", "生码状态", "生码类型", "生码时间" }, p =>
            {
                return new object[] { 
                     p.EnterpriseName,
					 p.Code,
					 p.Batch,
					 p.Gen_Code_Nums,
					 p.Query_Result_Content,
					 p.Query_Tel_Content,
					 p.Query_Message_Content,
					 p.Status,
					 p.TypeID,
					 p.ChangeDate		
				};
            }, "GenCode");
            return File(_byte_batch, "application/octet-stream", string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
        }
        /// <summary>
        /// 启用禁用
        /// </summary>
        /// <param name="flge"></param>
        /// <param name="CheckValue"></param>
        /// <returns></returns>
        public int GetViewFlag(int flge, string CheckValue)
        {
            string[] c = CheckValue.Split(',');
            int result = _CodeManageService.GetViewFlagService(flge, c,LogOnUser.EnterpriseId);
            return result;
        }
        #endregion

        #region 导码管理
        public ActionResult LeadShowData(int id)
        {
            var model = _Lecodeservice.GetLeadModelId(id);
            ViewBag.model = model;
            return View();
        }
        //导码
        public ActionResult LeadOut(string patch, string enterId, string batchId, string eName)
        {
            ViewBag.patch = patch;
            ViewBag.enterId = enterId;
            ViewBag.batchId = batchId;
            ViewBag.eName = eName;
            return View();
        }
        public ActionResult LeadCode()
        {
            return View();
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult LeadCodePageList(string Lsearch, int pageindex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageindex;
            pm.PageSize = pageSize;
            if (LogOnUser.Distributor != 0)
            {
                pm.StrCondition = "  ChangeLoginID=" + LogOnUser.UserID;
            }
            else
            {
                if (LogOnUser.EnterpriseId != 1)
                {
                    if (string.IsNullOrEmpty(LogOnUser.JuniorEnterprise))
                    { pm.StrCondition = "  EnterpriseId  in (0)"; }
                    else
                    { pm.StrCondition = "  EnterpriseId in (" + LogOnUser.JuniorEnterprise + ")"; }
                }
                else
                { pm.StrCondition = " 1=1"; }
            }
            //pm.StrCondition += !string.IsNullOrWhiteSpace(Lsearch) ? " and ViewDel=0 and  Batch like '% " + Lsearch + "%'  " : Lsearch;
            pm.StrCondition += " and ViewDel=0 and  Batch like '%" + Lsearch + "%' ";
            var model = _Lecodeservice.GetLeadCodePageListService(ref pm, Lsearch);
            return Json(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int DelLeadCode(int id)
        {
            int result = _Lecodeservice.DelLeadCodeService(id);
            return result;
        }
        /// <summary>
        /// 启用禁用
        /// </summary>
        /// <param name="flge"></param>
        /// <param name="CheckValue"></param>
        /// <returns></returns>
        public int GetViewFlags(int flge, string CheckValue)
        {
            string[] c = CheckValue.Split(',');
            int result = _Lecodeservice.GetViewFlagsService(flge, c);
            return result;
        }

        #endregion

        #region 生码批次

        /// <summary>
        /// 批次编辑
        /// </summary>
        /// <param name="BID">批次ID</param>
        /// <returns></returns>
        public ActionResult EditBatch(int BID)
        
       {
            
            var model = _codebatchservice.GetcodebatchModelId(BID);
            ViewBag.model = model;
            return View();
        }

        public ActionResult CodeBatch()
        {
            return View();
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="bsearch"></param>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult CodeBatchPageList(DetailSearch Smodel, int pageindex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageindex;
            pm.PageSize = pageSize;

            if (LogOnUser.Distributor != 0)
            {
                pm.StrCondition = "  ChangeLoginID=" + LogOnUser.UserID;
            }
            else
            {
                if (LogOnUser.EnterpriseId != 1)
                {
                    if (string.IsNullOrEmpty(LogOnUser.JuniorEnterprise)) { pm.StrCondition = "  EnterpriseId  in (0)"; }
                    else { pm.StrCondition = "  EnterpriseId in (" + LogOnUser.JuniorEnterprise + ") and ViewDel=0 "; }
                }
                else { pm.StrCondition = " 1=1 and ViewDel=0 "; }
            }
            var model = _codebatchservice.GetAProductBatchService(ref pm, Smodel);
            return Json(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int DelCodeBatch(int id)
        {
            var model = _codebatchservice.GetMaxBatchModel(LogOnUser.EnterpriseId);
            if (model==null ||id != model.Id) return 0;
            int result = _codebatchservice.DelCodeBatchService(id);
            return result;
        }
       
          public int BatchEditSave( CodeManage_GenCode model)
        {
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.ChangeDate = DateTime.Now;
            int result = cardService.Update(model);
            return result;
        }


        #endregion

        #region 互查识别
        public ActionResult MutualCheck()
        {
            return View();
        }
        #endregion

        #region 码查询
        public ActionResult SearchCode()
        {
            return View();
        }
        #endregion

        #region 分页数据
        public ActionResult SearchCodeList(string Csearch)
        {
           

            if (LogOnUser.EnterpriseId == 1)
            {
                Csearch = Csearch.Trim();
                var model = scodeService.GetSearchCodeList(Csearch);
                return Json(model);
            }

            else
            {
                Csearch = Csearch.Trim();
                var models = scodeService.GetSearchCodeLists(Csearch,LogOnUser.EnterpriseId);
                return Json(models);

            }
          
        }
        #endregion


        #region 码禁用 查看
        public ActionResult CodeDisableCheck()
        {

            return View();
        }
        #endregion

        #region 码申请
        public async Task<ActionResult> ApplyCode()
        {
            return View();
        }


        /// <summary>
        /// 分页数据
        /// </summary>      
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetApplyList(DetailSearch Smodel, int pageIndex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            var result = acservive.GetApplyListService(pm, LogOnUser.EnterpriseId, Smodel);
            return Json(result);
        }


        public ActionResult ApplyCodeAdd()
        {
            ViewData["EnterpriseId"] = Enterprdrop(0);
            ViewData["ProductId"] = productdrop(0);
            ViewBag.Ename = LogOnUser.EnterpriseName;        
            return View();
        }
        /// <summary>
        /// 机构下拉
        /// </summary>
        /// <param name="EnterpriseId"></param>
        /// <returns></returns>
        public List<SelectListItem> Enterprdrop(int EnterpriseId)
        {

            List<SelectItem> result = _manager.GetDirectSuperior(LogOnUser.EnterpriseId).ToList();
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "--请选择--", Value = "" });

            for (int i = 0; i < result.Count; i++)
            {
                SelectListItem item = new SelectListItem() { Text = result[i].Name.ToString(), Value = result[i].Code.ToString() };
                items.Add(item);
            }
            return items;
        }
        //产品下拉

        public List<SelectListItem> productdrop(int ProductId)
        {
            List<Product_Product> result = acservive.GetproService(LogOnUser.EnterpriseId);
            List<SelectListItem> items = new List<SelectListItem>();

            if (result.Count == 0)
            {
                items.Add(new SelectListItem() { Text = "", Value = "" });
            }
            else
            {
                items.Add(new SelectListItem() { Text = "--请选择--", Value = "" });
            }
            for (int i = 0; i < result.Count; i++)
            {
                SelectListItem item = new SelectListItem()
                {
                    Text = result[i].ProductName,
                    Value = result[i].Id.ToString()
                };
                if (ProductId == result[i].Id)
                {
                    item.Selected = true;
                }
                items.Add(item);
            }
            return items;
        }


        //添加申请
        public int AddApply(L.EntityModel.CodeManage_Allot model)
        {
            if (model.ProductId == 0)
            {
                model.AllotProductName ="";
            }
            model.ApplyDate = DateTime.Now;
            model.ApplyUserId = LogOnUser.UserID;
            model.T_EnterpriseId = LogOnUser.EnterpriseId;
            model.T_EnterpriseName = LogOnUser.EnterpriseName;
            model.HasVerifie = 0;
            model.SerialNum = Constant.MSQSerialNum;
            return acservive.AddApplyService(model);
        }


        //获取当前企业的直接上级机构
        public JsonResult GetCurrentEntepriseDirectSuperior()
        {
            return Json(_manager.GetDirectSuperior(LogOnUser.EnterpriseId), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 取消/删除 码申请操作
        /// </summary>
        public void CancelApply(int id)
        {
            acservive.CancelApply(id);
        }

        public ActionResult ApplyCodeView()
        {
            return View();
        }
        #endregion

        #region 召回清单
        //public ActionResult ReList()
        //{
        //    return View();
        //}
        #endregion

        #region 码分配
        public ActionResult AllotCode()
        {
            return View();
        }
        public ActionResult AllotCodeAdd()
        {
            return View();
        }

        /// <summary>
        /// 审核框
        /// </summary>
        public ActionResult AllotCodeVerifie()
        {
            return View();
        }

        /// <summary>
        /// 查看框
        /// </summary>
        public ActionResult AllotCodeView()
        {
           
            return View();
        }
                                                                    
        /// <summary>
        /// 获取当前用户码分配列表
        /// </summary>
        public async Task<JsonResult> GetAllotCodePagedList(int pageIndex = 1, int pageSize = 10, string serialNum = null, string productName = null, string toEnterpriseName = null, int? verifieState = null)
        {
            var Result = await _CodeManageService.GetCurrentUserAllotCodePagedList(pageIndex, pageSize, LogOnUser.EnterpriseId, serialNum, productName, toEnterpriseName, verifieState);
            return Json(Result, JsonRequestBehavior.AllowGet);
            #region MyRegion
            //string _where = string.Empty;
            //if (!string.IsNullOrWhiteSpace(serialNum))
            //{
            //    _where += string.Format(" SerialNum LIKE '%{0}%'", serialNum);
            //}

            //if (!string.IsNullOrWhiteSpace(productName))
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} AllotProductName LIKE '%{0}%'", productName, _and);
            //}

            //if (!string.IsNullOrWhiteSpace(toEnterpriseName))
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} T_EnterpriseName LIKE '%{0}%'", toEnterpriseName, _and);
            //}

            //if (verifieState.HasValue)
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} HasVerifie = {0}", verifieState.Value, _and);
            //}

            //return Json(_CodeManageService.GetCurrentUserAllotCodePagedList(new PagerModel
            //{
            //    PageSize = pageSize,
            //    Page = pageIndex,
            //    StrCondition = _where
            //}, LogOnUser.EnterpriseId), JsonRequestBehavior.AllowGet); 
            #endregion
        }


        /// <summary>
        /// 导出EXCEL
        /// </summary>
        public ActionResult ExportExcelFromAllotCode(string key)
        {
            #region MyRegion
            //string _where = string.Empty;
            //if (!string.IsNullOrWhiteSpace(serialNum))
            //{
            //    _where += string.Format(" SerialNum LIKE '%{0}%'", serialNum);
            //}

            //if (!string.IsNullOrWhiteSpace(productName))
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} AllotProductName LIKE '%{0}%'", productName, _and);
            //}

            //if (!string.IsNullOrWhiteSpace(toEnterpriseName))
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} T_EnterpriseName LIKE '%{0}%'", toEnterpriseName, _and);
            //}

            //if (verifieState.HasValue)
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {
            //        _and = " AND ";
            //    }
            //    _where += string.Format(" {1} HasVerifie = {0}", verifieState.Value, _and);
            //}
            //if (!string.IsNullOrWhiteSpace(key))
            //{
            //    var _and = string.Empty;
            //    if (!string.IsNullOrWhiteSpace(_where))
            //    {

            //    }
            //    _where += string.Format(" and id in ("+key+")");
            //} 
            #endregion

            List<CodeManage_Allot> _result = _CodeManageService.ExportExcelFromAllotCode(key);
            var rs = Excel.Exprot<CodeManage_Allot>(_result,
                 new string[] { "流水号", "分配机构", "申请机构", "产品", "产品规格", "分配批次", "开始码", "结束码", "数量", "申请时间", "分配时间", "申请人", "申请理由", "审核状态", "审核说明" },
                 p =>
                 {
                     return new object[] { 
                       p.SerialNum,
                       p.F_EnterpriseName,
                       p.T_EnterpriseName,
                       p.AllotProductName,
                       p.ApplyProductSize,
                       p.AllotBatch,
                       p.StartCode,
                       p.EndCode,
                       p.AllotCount,
                       p.ApplyDate,
                       p.AllotDate,
                       p.ApplyUserName,
                       p.ApplyReason,
                       p.HasVerifie==0?"末审核":p.HasVerifie == 1?"通过":"不通过",
                       p.VerificationNotes
                };
                 }, "AllotCode");
            return File(rs, "application/octet-stream", Guid.NewGuid().ToString() + ".xlsx");
        }

        /// <summary>
        /// 不/拒绝 通过审核
        /// </summary>
        public void RefuseAllotCode(int id, string reason = null)
        {
            _CodeManageService.RefuseAllotCode(id, reason);
        }

        /// <summary>
        /// 获取可用批次
        /// </summary>
        public JsonResult GetGenCodeBatchList(int allotType = 0)
        {
            var result = _CodeManageService.GetGenCodeBatchList(LogOnUser.EnterpriseId, allotType);
            return Json(result,JsonRequestBehavior.AllowGet);
        }
        //public string Getapplytype()
        //{
        //    var result = _CodeManageService.Getapplytype(LogOnUser.EnterpriseId);
        //    return result;
        //}
        //分配保存
        [HttpPost]
        public JsonResult AllotCodeAddOp([System.Web.Http.FromBody]CodeManage_Allot model, bool notSkip = true, bool isAdd = false)
        {
            if (model == null)
            {
                return Json("当前无法执行该操作");
            }
            
            model.AllotDate = DateTime.Now;
            model.F_EnterpriseId = LogOnUser.EnterpriseId;
            model.F_EnterpriseName = LogOnUser.EnterpriseName;
            model.AllotDate = DateTime.Now;
            if (isAdd)
            {
                model.F_EnterpriseId = LogOnUser.EnterpriseId;
                model.F_EnterpriseName = LogOnUser.EnterpriseName;
                model.ApplyUserId = LogOnUser.UserID;        //主动分配填写当前登录人ID
                model.ApplyUserName = LogOnUser.LoginName;   //主动分配填写当前登录人
                model.ApplyDate = DateTime.Now;              //主动分配填写当前时间
                model.ApplyReason = "主动分配";
                model.HasVerifie = 1;
                model.VerificationNotes = "主动分配，自动审核通过";
                model.SerialNum = Constant.MFPSerialNum;
                model.CodeFunc = "1,2";
                model.AllotDate = DateTime.Now;
            }

            bool result = _CodeManageService.CodeAllotAdd(model, notSkip, isAdd);

            if (!result)
            {
                return Json("请检查该批次码状态是否绑定产品或已包含分配项或者码是否已禁用！");
            }
            return Json(true);
        }


        /// <summary>
        /// 获取当前机构的直接子级机构
        /// </summary>
        public JsonResult GetCurrentEntepriseDirectSubLevel()
        {
            return Json(_manager.GetDirectSubLevel(LogOnUser.EnterpriseId), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取产品下拉列表
        /// </summary>
        public JsonResult GetProductNameList(int enterpriseId, int? typeId = null)
        {
            return Json(_ProductBatchService.GetProductNameList(enterpriseId, typeId), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 计算整版数量
        /// </summary>
        public int GetWholeBatchCount(string batchNo)
        {
            return _CodeManageService.GetWholeBatchCount(LogOnUser.ECode, batchNo);
        }
       
        //判断是否是admin登录
        public int Isadmin(int EnterpriseId)
        {
            return _CodeManageService.Isadmin(LogOnUser.EnterpriseId);
        }
        //判断是否是已生码成功
      
        public int IsBornCode(int EnterpriseId,int batchid)
        {
            return _CodeManageService.IsBornCode(EnterpriseId, batchid);
        }
        public int SetAllotPass(int allotId)
        {
            var time = DateTime.Now.ToString();
            return _CodeManageService.SetAllotPass(allotId, LogOnUser.EnterpriseId, LogOnUser.EnterpriseName, time);
        }
        #endregion
    }

}